package com.zq.qingzhu.controller.admin;

import com.zq.qingzhu.bean.Admin;
import com.zq.qingzhu.common.ServiceResultEnum;
import com.zq.qingzhu.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Resource
	private AdminService adminService;

	@GetMapping({"/login"})
	public String login() {
		return "admin/login";
	}

	@GetMapping({"/test"})
	public String test() {
		return "admin/test";
	}


	@GetMapping({"", "/", "/index", "/index.html"})
	public String index(HttpServletRequest request) {
		request.setAttribute("path", "index");
		request.setAttribute("categoryCount", 0);
		request.setAttribute("blogCount", 0);
		request.setAttribute("linkCount", 0);
		request.setAttribute("tagCount", 0);
		request.setAttribute("commentCount", 0);
		request.setAttribute("path", "index");
		return "admin/index";
	}

	@PostMapping(value = "/login")
	public String login(@RequestParam("userName") String userName,
						@RequestParam("password") String password,
						HttpSession session) {
		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
			session.setAttribute("errorMsg", "用户名或密码不能为空");
			return "admin/login";
		}

		Admin admin = adminService.login(userName, password);
		if (admin != null) {
		//			session.setAttribute("loginUser", admin.getNickName());
			session.setAttribute("loginUserId",admin.getId());
			session.setAttribute("loginUser",admin.getUsername());
			//session过期时间设置为7200秒 即两小时
			//session.setMaxInactiveInterval(60 * 60 * 2);
			return "redirect:/admin/index";
		} else {
			session.setAttribute("errorMsg", "登陆失败！！！");
			return "admin/login";
		}
	}

	@GetMapping("/profile")
	public String profile(HttpServletRequest request) {
		Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
		Admin admin = adminService.getUserDetailById(loginUserId);
		if (admin == null) {
			return "admin/login";
		}
		request.setAttribute("path", "profile");
		request.setAttribute("loginUserName", admin.getUsername());
//		request.setAttribute("nickName", admin.getNickName());
		return "admin/profile";
	}

	@PostMapping("/profile/password")
	@ResponseBody
	public String passwordUpdate(HttpServletRequest request,
								 @RequestParam("originalPassword") String originalPassword,
								 @RequestParam("newPassword") String newPassword) {
		if (StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(newPassword)) {
			return "参数不能为空";
		}
		Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
		if (adminService.updatePassword(loginUserId, originalPassword, newPassword)) {
			//修改成功后清空session中的数据，前端控制跳转至登录页
			request.getSession().removeAttribute("loginUserId");
			request.getSession().removeAttribute("loginUser");
			request.getSession().removeAttribute("errorMsg");
			return ServiceResultEnum.SUCCESS.getResult();
		} else {
			return "修改失败";
		}
	}

	@PostMapping("/profile/name")
	@ResponseBody
	public String nameUpdate(HttpServletRequest request, @RequestParam("loginUserName") String loginUserName,
							 @RequestParam("nickName") String nickName) {
		if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
			return "参数不能为空";
		}
		Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
		if (adminService.updateName(loginUserId, loginUserName, nickName)) {
			return ServiceResultEnum.SUCCESS.getResult();
		} else {
			return "修改失败";
		}
	}

	@GetMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("loginUserId");
		request.getSession().removeAttribute("loginUser");
		request.getSession().removeAttribute("errorMsg");
		return "admin/login";
	}
}
